package com.zjw.zy.componse.demo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zjw.zy.componse.demo.utils.RotateGestureDetector;

/**
 * @author ：zhong.jw
 * @date ：Created in 2023/2/24 9:23
 */
public class RectView extends FrameLayout {

    private static final String TAG = "RectView";

    private View mRotateView;

    private final ScaleGestureDetector scaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override
        public boolean onScale(@NonNull ScaleGestureDetector detector) {
            Log.d(TAG, "onScale() called with: detector = [" + detector.getScaleFactor() + "]");
            mRotateView.setScaleX(detector.getScaleFactor());
            mRotateView.setScaleY(detector.getScaleFactor());
            return true;
        }

        @Override
        public boolean onScaleBegin(@NonNull ScaleGestureDetector detector) {
            return super.onScaleBegin(detector);
        }

        @Override
        public void onScaleEnd(@NonNull ScaleGestureDetector detector) {
            super.onScaleEnd(detector);
        }
    });

    private final RotateGestureDetector rotateGestureDetector = new RotateGestureDetector(new RotateGestureDetector.Listener() {
        @Override
        public void onRotateStart(RotateGestureDetector detector) {

        }

        @Override
        public void onRotating(RotateGestureDetector detector) {
            mRotateView.setRotation((float) detector.eulerAngle);
        }

        @Override
        public void onRotateEnd(RotateGestureDetector detector) {

        }
    });

    public RectView(Context context) {
        super(context);

        mRotateView = new View(context);
        mRotateView.setBackgroundColor(Color.GRAY);

        this.addView(mRotateView, new FrameLayout.LayoutParams(100, 100, Gravity.CENTER));
    }

    public RectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

//    @SuppressLint("ClickableViewAccessibility")
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        rotateGestureDetector.onTouchEvent(event);
//        return true;
//    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        rotateGestureDetector.onTouchEvent(ev);
        scaleGestureDetector.onTouchEvent(ev);
        return true;
    }
}
